package com.jy.modules.cims.component.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleBeanUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.ImgurlUtils;
import com.jy.modules.cims.component.common.service.TCMmContractService;
import com.jy.modules.cims.component.common.service.TCMmCreditDetailService;
import com.jy.modules.cims.component.common.service.TCMmImgurlService;
import com.jy.modules.cims.component.common.service.TCMmRefundPlanService;
import com.jy.modules.cims.component.loan.ICLoan;
import com.jy.modules.cims.component.quota.ICQuota;
import com.jy.modules.cims.data.common.dto.TCMmCostPlanDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmImgurlDTO;
import com.jy.modules.cims.data.common.dto.TCMmRefundPlanDTO;
import com.jy.modules.cims.data.dao.AttachDao;
import com.jy.modules.cims.data.dao.CostPlanDao;
import com.jy.modules.cims.data.dao.CreditDetailDao;
import com.jy.modules.cims.data.dao.DealDao;
import com.jy.modules.cims.data.dao.QuotaDao;
import com.jy.modules.cims.data.dto.SearchCreditDetailDto;
import com.jy.modules.cims.data.dto.credit.CostPlanDto;
import com.jy.modules.cims.data.dto.inter.tbj.jj.AttachContent;
import com.jy.modules.cims.data.dto.inter.tbj.jj.AttachFile;
import com.jy.modules.cims.data.dto.loan.LoanBorrowerAssetDto;
import com.jy.modules.cims.data.dto.loan.LoanBorrowerCreditInfoDto;
import com.jy.modules.cims.data.dto.loan.LoanBorrowerDto;
import com.jy.modules.cims.data.dto.loan.LoanBorrowerEntityDto;
import com.jy.modules.cims.data.dto.loan.LoanBorrowerJobDto;
import com.jy.modules.cims.data.dto.loan.LoanBorrowerLoanInfoDto;
import com.jy.modules.cims.data.dto.loan.LoanDealBaseInfoDto;
import com.jy.modules.cims.data.interact.api.ResponseMessageEnum;
import com.jy.modules.cims.data.interact.api.request.RequestAttachSearchDTO;
import com.jy.modules.cims.data.interact.api.request.RequestCreditBaseSearchDTO;
import com.jy.modules.cims.data.interact.api.request.RequestCreditRepaymentNoticeCreditDTO;
import com.jy.modules.cims.data.interact.api.request.RequestCreditRepaymentNoticeDTO;
import com.jy.modules.cims.data.interact.api.request.RequestLoanBaseSearchDTO;
import com.jy.modules.cims.data.interact.api.request.RequestLoanDetailSearchDTO;
import com.jy.modules.cims.data.interact.api.request.RequestLoanRepaymentNoticeDTO;
import com.jy.modules.cims.data.interact.api.request.RequestLoanRepaymentNoticeLoanDTO;
import com.jy.modules.cims.data.interact.api.request.RequestRefundPlanSearchDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseAttachSearchDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseAttachSearchFileDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseCreditBaseSearchDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseCreditBaseSearchDetailDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseCreditRepaymentNoticeCreditDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseCreditRepaymentNoticeDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanBaseInfo;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanBaseSearchDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanDetailBorrower;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanDetailCreditInfo;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanDetailEntity;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanDetailHouse;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanDetailJob;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanDetailLoanInfo;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanDetailSearchDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanRepaymentNoticeDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanRepaymentNoticeLoanDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseRefundPlanSearchDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseRefundPlanSearchPlanDTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response8003DTO;
import com.jy.modules.cims.interact.ICInteract;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.QueryReqBean;

/**
 * @description 捷越接受外围系统查询请求组件。
 * @author shengzhoutao
 * 
 */
@Service("com.jy.modules.cims.component.api.CReceiveSearchImpl")
public class CReceiveSearchImpl {

	private static final Logger logger = LoggerFactory.getLogger(CReceiveSearchImpl.class);

	@Autowired
	public QuotaDao quotaDao;

	@Autowired
	private AttachDao attachDao;

	@Autowired
	public DealDao dealDao;

	@Autowired
	public CostPlanDao costPlanDao;

	@Autowired
	public CreditDetailDao creditDetailDao;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmContractService")
	public TCMmContractService tcMmContractService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.quota.CQuotaImpl")
	public ICQuota iCQuota;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.loan.CLoanImpl")
	private ICLoan loanImpl;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.ImgurlUtils")
	private ImgurlUtils imgurlUtils;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmRefundPlanService")
	private TCMmRefundPlanService refundPlanService;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCreditDetailService")
	private TCMmCreditDetailService creditDetailService;

	@Autowired
    @Qualifier("com.jy.modules.cims.component.common.service.TCMmImgurlService")
    private TCMmImgurlService imgurlService;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CCInteractImpl")
	public ICInteract iCInteract;
	
	/**
	 * @description 接受商户查询进件列表请求
	 * @param
	 * @return
	 * @throws Exception
	 * @author tianmaoshui
	 * @Date
	 */
	public ResponseLoanBaseSearchDTO searchLoanBase(final RequestLoanBaseSearchDTO content) throws Exception {
		ResponseLoanBaseSearchDTO response = new ResponseLoanBaseSearchDTO();
		String quotaCode = content.getQuotaCode();
		String dealCode = content.getDealCode();
		if (!SimpleStringUtils.hasText(quotaCode) && !SimpleStringUtils.hasText(dealCode)) {
			response.setMessage("额度申请编号:quotaCode和批次号:dealCode不能同时为空！");
			response.setResult(ResponseMessageEnum.FAILURE.getResult());// 交易失败
			return response;
		}
		
		LoanDealBaseInfoDto dto = new LoanDealBaseInfoDto();
		StringBuilder sb = new StringBuilder();
		String id = null;
		if (SimpleStringUtils.hasText(quotaCode)) {
			quotaCode = quotaCode.trim();
			dto.setQuotaCode(quotaCode);
			sb.append(",额度编号:").append(quotaCode);
			id = quotaCode;
		}
		if (SimpleStringUtils.hasText(dealCode)) {
			dealCode = dealCode.trim();
			dto.setDealCode(dealCode);
			sb.append(",批次号:").append(dealCode);
			id = dealCode;
		} 
        
		logger.info("进件列表查询START:" + sb.substring(1));
		//分页
		QueryReqBean params = new QueryReqBean();
		params.setSearchParams(SearchParamUtil.getSearchParam(dto));
		PageParameter pageInfo = new PageParameter();
		pageInfo.setCurrentPage(content.getPageIndex());
		pageInfo.setPageSize(content.getPageSize());
		params.setPageParameter(pageInfo);
		
		List<LoanDealBaseInfoDto> list = loanImpl.getLoanDealBaseInfoListByPaging(params.getSearchParams());
		List<ResponseLoanBaseInfo> contractList = null;
		if (CollectionsUtils.isNotEmpty(list)) {
			contractList = new ArrayList<ResponseLoanBaseInfo>(list.size());
			ResponseLoanBaseInfo responseDto = null;
			for (LoanDealBaseInfoDto loanDealBaseInfoDto : list) {
				responseDto = new ResponseLoanBaseInfo();
				SimpleBeanUtils.copyBean2Bean(responseDto, loanDealBaseInfoDto);
				contractList.add(responseDto);
			}
			
			logger.info("进件列表查询END:" + sb.substring(1));
			response.setId(id);
			response.setTotalCount(pageInfo.getTotalCount());
			response.setContractList(contractList);
			response.setMessage("进件列表查询成功。" + sb.substring(1));
			response.setResult(ResponseMessageEnum.SUCCESS.getResult());// 交易成功
			return response;
		} else {
			contractList = Collections.emptyList();
			response.setId(id);
			response.setContractList(contractList);
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}
	}

	/**
	 * @description 接受商户查询进件详细信息请求
	 * @param
	 * @return
	 * @throws Exception
	 * @author tianmaoshui
	 * @Date
	 */
	public ResponseLoanDetailSearchDTO searchLoanDetail(final RequestLoanDetailSearchDTO content) throws Exception {

		ResponseLoanDetailSearchDTO response = new ResponseLoanDetailSearchDTO();
		String loanNumber = content.getLoanNumber();
		String  transferId = content.getTransferId();
		response.setId(loanNumber);
		response.setTransferId(transferId);

		//Map<String, Object> paramMap = new HashMap<String, Object>();
		logger.info("进件详细信息查询START: 合同编号:" + loanNumber+"债权编号:"+transferId);
		LoanBorrowerDto loanBorrowerDto = loanImpl.getLoanBorrowerInfoByLoanNumber(loanNumber);
		//借款人基本信息
		
		if (null == loanBorrowerDto) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}
		
		ResponseLoanDetailBorrower borrower = new ResponseLoanDetailBorrower();
		SimpleBeanUtils.copyBean2Bean(borrower, loanBorrowerDto);
		if(null != loanBorrowerDto.getBirthday()){
			borrower.setBirthday(DateTimeUtils.formatDate(loanBorrowerDto.getBirthday(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
		}
		if(null != loanBorrowerDto.getIdPeriod()){
			borrower.setIdPeriod(DateTimeUtils.formatDate(loanBorrowerDto.getIdPeriod(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
		}
		if(null != loanBorrowerDto.getSignDate()){
			borrower.setSignDate(DateTimeUtils.formatDate(loanBorrowerDto.getSignDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
		}
		if(null != loanBorrowerDto.getApprDate()){
			borrower.setApprDate(DateTimeUtils.formatDate(loanBorrowerDto.getApprDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
		}
		if (null == loanBorrowerDto.getAffirmDateTime()) {
			try {
				Response8003DTO response8003DTO = iCInteract.getBorrowersResult(loanNumber);
				String affirmDateTime = response8003DTO.getBorrower().getAffirmDateTime();
				if (SimpleStringUtils.hasText(affirmDateTime)) {
					borrower.setAffirmDateTime(affirmDateTime);
				}
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		response.setBorrower(borrower);
		//借款人工作
		List<ResponseLoanDetailJob> jobs = new ArrayList<ResponseLoanDetailJob>();
		List<LoanBorrowerJobDto> jobList = loanImpl.getLoanBorrowerJobsByLoanNumber(loanNumber);
		if (CollectionsUtils.isNotEmpty(jobList)) {
			ResponseLoanDetailJob job = null;
			for (int i = 0; i < jobList.size(); i++) {
				job = new ResponseLoanDetailJob();
				SimpleBeanUtils.copyBean2Bean(job, jobList.get(i));
				if(null != jobList.get(i).getStartDate()){
					job.setStartDate(DateTimeUtils.formatDate(jobList.get(i).getStartDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
				}
				jobs.add(job);
			}
		}
		response.setJobs(jobs);
		//经营实体明细
		List<ResponseLoanDetailEntity> entities = new ArrayList<ResponseLoanDetailEntity>();
		List<LoanBorrowerEntityDto> entityList = loanImpl.getLoanBorrowerEntityByLoanNumber(loanNumber);
		if (CollectionsUtils.isNotEmpty(entityList)) {
			ResponseLoanDetailEntity e = null;
			for (int i = 0; i < entityList.size(); i++) {
				e = new ResponseLoanDetailEntity();
				SimpleBeanUtils.copyBean2Bean(e, entityList.get(i));
				if(null != entityList.get(i).getFoundedDate()){
					e.setFoundedDate(DateTimeUtils.formatDate(entityList.get(i).getFoundedDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
				}
				entities.add(e);
			}
		}
		response.setEntity(entities);
		//借款人资产明细
		List<ResponseLoanDetailHouse> assets = new ArrayList<ResponseLoanDetailHouse>();
		List<LoanBorrowerAssetDto> assetList = loanImpl.getLoanBorrowerAssetByLoanNumber(loanNumber);
		if (CollectionsUtils.isNotEmpty(assetList)) {
			ResponseLoanDetailHouse asset = null;
			for (int i = 0; i < assetList.size(); i++) {
				asset = new ResponseLoanDetailHouse();
				SimpleBeanUtils.copyBean2Bean(asset, assetList.get(i));
				if(null != assetList.get(i).getBuyTime()){
					asset.setBuyTime(DateTimeUtils.formatDate(assetList.get(i).getBuyTime(), DateTimeUtils.DEFAULT_DATE_TIME_FORMAT));
				}
				assets.add(asset);
			}
		}
		response.setAsset(assets);
		//征信信息
		ResponseLoanDetailCreditInfo creditInfo = new ResponseLoanDetailCreditInfo();
		LoanBorrowerCreditInfoDto credit = loanImpl.getLoanBorrowerCreditInfoByLoanNumber(loanNumber);
		if(credit!=null){
			SimpleBeanUtils.copyBean2Bean(creditInfo, credit);
			response.setCreditInfo(creditInfo);
			
		}
		
		//放款信息
		ResponseLoanDetailLoanInfo loanInfo = new ResponseLoanDetailLoanInfo();
		
		if(SimpleStringUtils.hasText(transferId)){//债权类查询
			response.setLoanInfo(loanInfo);
		}else{//合同类
			LoanBorrowerLoanInfoDto loanInfoDto = loanImpl.getLoanBorrowerLoanInfoByLoanNumber(loanNumber);
			if(loanInfoDto==null){
				response.setResult(ResponseMessageEnum.FAILURE.getResult());
				response.setMessage("未查询到放款记录！");
				return response;
			}
			SimpleBeanUtils.copyBean2Bean(loanInfo, loanInfoDto);
			response.setLoanInfo(loanInfo);
		}
		response.setMessage("进件详细信息查询成功。合同编号:" + loanNumber+"债权编号："+transferId);
		response.setResult(ResponseMessageEnum.SUCCESS.getResult());// 交易成功
		logger.info("进件详细信息查询END: 合同编号:" + loanNumber+"债权编号："+transferId);
		return response;
	}

	/**
	 * @description 接受商户查询进件附件信息请求
	 * @param
	 * @return
	 * @throws Exception
	 * @author lizhiqiang
	 * @Date
	 */
	public ResponseAttachSearchDTO searchLoanAttach(final RequestAttachSearchDTO content) throws Exception {

		ResponseAttachSearchDTO response = new ResponseAttachSearchDTO();

		AttachContent attachContent = new AttachContent();
		attachContent.setStreamId(content.getLoanNumber());
		List<AttachFile> files = attachDao.findAttachFile(attachContent);
		List<ResponseAttachSearchFileDTO> fileList = new ArrayList<ResponseAttachSearchFileDTO>();
		if (CollectionsUtils.isNotEmpty(files)) {
			ResponseAttachSearchFileDTO af = null;
			for (AttachFile a : files) {
				af = new ResponseAttachSearchFileDTO();
				af.setFileCategory(imgurlUtils.getAttachType(a.getFileName()));
				af.setFileType(1);
				af.setFileName(a.getFileName());
				af.setFileRealName(a.getFileRealName());
				fileList.add(af);
			}
			
			response.setFile(fileList);
			response.setId(content.getLoanNumber());
			response.setResult(ResponseMessageEnum.SUCCESS.getResult());
			response.setFilePath(dealDao.getLoanFtpPath(content.getLoanNumber()));
		} else {
			response.setId(content.getLoanNumber());
			response.setMessage("没有相关附件信息");
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
		}
		
		return response;
	}

	/**
	 * @description 接受商户查询债权附件信息请求
	 * @param RequestAttachSearchDTO  content
	 * @return ResponseAttachSearchDTO
	 * @throws Exception
	 * @author shengzhoutao
	 * @Date 2016-12-23
	 */
	public ResponseAttachSearchDTO searchCreditAttach(final RequestAttachSearchDTO content) throws Exception {

		ResponseAttachSearchDTO response = new ResponseAttachSearchDTO();
		
		TCMmCreditDetailDTO creditDetail = new TCMmCreditDetailDTO();
		creditDetail.setTransferId(content.getTransferId());
		creditDetail.setCreditCode(content.getLoanNumber());
		List<TCMmCreditDetailDTO> creditDetailList = creditDetailService.searchTCMmCreditDetail(SearchParamUtil.getSearchParam(creditDetail));
		creditDetail = null;
		if (CollectionsUtils.isEmpty(creditDetailList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		TCMmImgurlDTO imgurl = new TCMmImgurlDTO();
		imgurl.setContractNo(content.getLoanNumber());
		List<TCMmImgurlDTO> imgurlList = imgurlService.searchTCMmImgurl(SearchParamUtil.getSearchParam(imgurl));
		imgurl = null;
		if (CollectionsUtils.isEmpty(imgurlList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		List<ResponseAttachSearchFileDTO> fileList = new ArrayList<ResponseAttachSearchFileDTO>();
		ResponseAttachSearchFileDTO file = null;
		for (TCMmImgurlDTO img : imgurlList) {
			file = new ResponseAttachSearchFileDTO();
			file.setFileCategory(imgurlUtils.getAttachType(img.getAttachName()));
			file.setFileType(1);
			file.setFileName(img.getAttachName());
			String attachUrl = img.getAttachUrl();
			String[] fileUrl = attachUrl.split("/");
			file.setFileRealName(fileUrl[fileUrl.length - 1]);

			fileList.add(file);
		}
		file = null;
		
		response.setFile(fileList);
		response.setId(content.getTransferId());
		response.setResult(ResponseMessageEnum.SUCCESS.getResult());
		response.setFilePath(creditDetailList.get(0).getAccessoryFtpUrl());
		return response;
	}

	/**
	 * @description 接受商户查询还款计划信息请求
	 * @param content
	 * @return response
	 * @throws Exception
	 * @author shengzhoutao
	 * @Date 2016-11-28
	 */
	public ResponseRefundPlanSearchDTO searchRefundPlan(final RequestRefundPlanSearchDTO content) throws Exception {
		ResponseRefundPlanSearchDTO response = new ResponseRefundPlanSearchDTO();
		response.setId(content.getLoanNumber());

		TCMmRefundPlanDTO refundPlan = new TCMmRefundPlanDTO();
		refundPlan.setCreditorCode(content.getLoanNumber());
		List<TCMmRefundPlanDTO> dataList = refundPlanService.searchTCMmRefundPlan(SearchParamUtil.getSearchParam(refundPlan));
		refundPlan = null;
		if (CollectionsUtils.isEmpty(dataList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		TCMmCreditDetailDTO creditDetailDTO = new TCMmCreditDetailDTO();
		creditDetailDTO.setCreditCode(content.getLoanNumber());
		List<TCMmCreditDetailDTO> creditDetailList = creditDetailService.searchTCMmCreditDetail(SearchParamUtil.getSearchParam(creditDetailDTO));
		creditDetailDTO = null;

		List<ResponseRefundPlanSearchPlanDTO> planList = new ArrayList<ResponseRefundPlanSearchPlanDTO>();
		ResponseRefundPlanSearchPlanDTO planDTO = null;
		for (TCMmRefundPlanDTO tcMmRefundPlanDTO : dataList) {
			planDTO = new ResponseRefundPlanSearchPlanDTO();
			planDTO.setLoanTerms(tcMmRefundPlanDTO.getCurrentTime());
			planDTO.setPaymentDate(DateTimeUtils.formatDate(tcMmRefundPlanDTO.getStartDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
			planDTO.setLoanDate(DateTimeUtils.formatDate(creditDetailList.get(0).getInitialLoanDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
			planDTO.setMustAmount(tcMmRefundPlanDTO.getMustAmount());
			planDTO.setMustAccrual(tcMmRefundPlanDTO.getMustAccrual());
			planDTO.setThisRepaySum(tcMmRefundPlanDTO.getThisRepaySum());
			planList.add(planDTO);
		}

		response.setResult(ResponseMessageEnum.SUCCESS.getResult());
		response.setPlanList(planList);
		return response;
	}

	/**
	 * @description 接受商户查询查询债权信息请求
	 * @param
	 * @return
	 * @throws Exception
	 * @author
	 * @Date
	 */
	public ResponseCreditBaseSearchDTO searchCreditBase(final RequestCreditBaseSearchDTO content) throws Exception {
		logger.info("接受商户查询查询债权信息>>开始");
		ResponseCreditBaseSearchDTO response = new ResponseCreditBaseSearchDTO();
		SearchCreditDetailDto searchDto = new SearchCreditDetailDto();
		String dealCode = content.getDealCode();
		String quotaCode = content.getQuotaCode();
		if(!SimpleStringUtils.hasText(dealCode)&&!SimpleStringUtils.hasText(quotaCode)){
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("批次号，额度号同时为空！");
			return response;
		}
		searchDto.setDealCode(dealCode);
		searchDto.setQuotaCode(quotaCode);
		
		//分页查询
		QueryReqBean params = new QueryReqBean();
		params.setSearchParams(SearchParamUtil.getSearchParam(searchDto));
		PageParameter pageInfo = new PageParameter();
		pageInfo.setCurrentPage(content.getPageIndex());
		pageInfo.setPageSize(content.getPageSize());
		params.setPageParameter(pageInfo);
		List<SearchCreditDetailDto> list = creditDetailDao.searchCreditDetailBaseByPaging(params.getSearchParams());
		if(CollectionsUtils.isEmpty(list)){
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("债权明细列表查询为空！");
			return response;
		}
		List<ResponseCreditBaseSearchDetailDTO> creditList = new ArrayList<ResponseCreditBaseSearchDetailDTO>();
		ResponseCreditBaseSearchDetailDTO responseDetailDTO= null;
		for (SearchCreditDetailDto searchCreditDetailDto : list) {
			responseDetailDTO = new ResponseCreditBaseSearchDetailDTO();
			SimpleBeanUtils.copyBean2Bean(responseDetailDTO, searchCreditDetailDto);

			responseDetailDTO.setProductCode(CimsConstant.PRODUCT_TYPE_CREDIT);//信用贷
			if(CimsConstant.PRODUCT_TYPE_CREDIT.endsWith(searchCreditDetailDto.getProductCode())){
				responseDetailDTO.setProductCode(CimsConstant.PRODUCT_TYPE_CREDIT);//信用贷
			}else{
				responseDetailDTO.setProductCode("02");//抵押贷
			}
			
			
			creditList.add(responseDetailDTO);
		}
		if(SimpleStringUtils.hasText(quotaCode)) {
			response.setId(quotaCode);
		}
		if(SimpleStringUtils.hasText(dealCode)) {
			response.setId(dealCode);
		}
		response.setTotalCount(pageInfo.getTotalCount());
		response.setCreditList(creditList);
		response.setResult(ResponseMessageEnum.SUCCESS.getResult());
		response.setMessage("债权明细列表查询完毕！");
		logger.info("接受商户查询查询债权信息>>完毕");
		return response;
	}

	/**
	 * @description 接受贷款还款通知请求
	 * @param
	 * @return
	 * @throws Exception
	 * @author
	 * @Date
	 */
	public ResponseLoanRepaymentNoticeDTO searchLoanCostPlan(final RequestLoanRepaymentNoticeDTO content) throws Exception {
		ResponseLoanRepaymentNoticeDTO response = new ResponseLoanRepaymentNoticeDTO();

		List<RequestLoanRepaymentNoticeLoanDTO> searchList = content.getLoanList();
		if (CollectionsUtils.isEmpty(searchList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		if (searchList.size() > 100) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("贷款列表条数超过100条");
			return response;
		}

		List<CostPlanDto> costPlanDtoList = new ArrayList<CostPlanDto>();
		CostPlanDto costPlanDto = null;
		for (RequestLoanRepaymentNoticeLoanDTO loan : searchList) {
			costPlanDto = new CostPlanDto();
			costPlanDto.setTransferId(loan.getLoanNumber());
			costPlanDto.setCreditorCode(loan.getLoanNumber());
			costPlanDto.setStartDate(loan.getPaymentDate());
			costPlanDto.setEndDate(loan.getPaymentDate());
			if (0 != loan.getLoanTerms()) {
				costPlanDto.setCurrentTime(Long.parseLong(String.valueOf(loan.getLoanTerms())));
			}
			costPlanDtoList.add(costPlanDto);
		}

		CostPlanDto searchCostPlanDto = new CostPlanDto();
		searchCostPlanDto.setPlanStatus(CimsConstant.CREDIT_SETTLE_STATUS);
		searchCostPlanDto.setCostPlanDtoList(costPlanDtoList);
		List<TCMmCostPlanDTO> dataList = costPlanDao.searchTCMmCostPlan(SearchParamUtil.getSearchParam(searchCostPlanDto));
		if (CollectionsUtils.isEmpty(dataList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		List<ResponseLoanRepaymentNoticeLoanDTO> loanList = new ArrayList<ResponseLoanRepaymentNoticeLoanDTO>();// 贷款列表
		ResponseLoanRepaymentNoticeLoanDTO loan = null;
		Map<String, String> tempMap = new HashMap<String, String>();
		for (TCMmCostPlanDTO costPlanDTO : dataList) {
			if (SimpleStringUtils.hasText(tempMap.get(costPlanDTO.getTransferId()))) {
				continue;
			}

			loan = new ResponseLoanRepaymentNoticeLoanDTO();
			SimpleBeanUtils.copyBean2Bean(loan, costPlanDTO);
			loan.setLoanNumber(costPlanDTO.getTransferId());
			loan.setLoanTerms(costPlanDTO.getCurrentTime().intValue());
			loan.setPaymentDate(DateTimeUtils.formatDate(costPlanDTO.getStartDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
			if (SimpleStringUtils.hasText(costPlanDTO.getCreditStatus())) {
				loan.setAdvanceSettleValue(costPlanDTO.getMatchAmount().add(costPlanDTO.getReturnAmount()));
				tempMap.put(costPlanDTO.getTransferId(), costPlanDTO.getTransferId());
			}

			loanList.add(loan);
		}

		if (CollectionsUtils.isEmpty(loanList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		response.setId("LRN" + DateTimeUtils.formatDate(new Date(), DateTimeUtils.DEFAULT_DATE_TIME_FORMAT_2));
		response.setResult(ResponseMessageEnum.SUCCESS.getResult());
		response.setLoanList(loanList);
		return response;
	}

	/**
	 * @description 接受债权还款通知请求
	 * @param
	 * @return
	 * @throws Exception
	 * @author
	 * @Date
	 */
	public ResponseCreditRepaymentNoticeDTO searchCreditCostPlan(final RequestCreditRepaymentNoticeDTO content) throws Exception {
		ResponseCreditRepaymentNoticeDTO response = new ResponseCreditRepaymentNoticeDTO();

		List<RequestCreditRepaymentNoticeCreditDTO> searchList = content.getCreditList();
		if (CollectionsUtils.isEmpty(searchList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		if (searchList.size() > 100) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("债权列表条数超过100条");
			return response;
		}

		List<CostPlanDto> costPlanDtoList = new ArrayList<CostPlanDto>();

		CostPlanDto costPlanDto = null;
		for (RequestCreditRepaymentNoticeCreditDTO credit : searchList) {
			costPlanDto = new CostPlanDto();
			costPlanDto.setTransferId(credit.getTransferId());
			costPlanDto.setCreditorCode(credit.getCreditCode());
			costPlanDto.setStartDate(credit.getPaymentDate());
			costPlanDto.setEndDate(credit.getPaymentDate());
			if (0 != credit.getLoanTerms()) {
				costPlanDto.setCurrentTime(Long.parseLong(String.valueOf(credit.getLoanTerms())));
			}
			costPlanDtoList.add(costPlanDto);
		}

		CostPlanDto searchCostPlanDto = new CostPlanDto();
		searchCostPlanDto.setPlanStatus(CimsConstant.CREDIT_SETTLE_STATUS);
		searchCostPlanDto.setCostPlanDtoList(costPlanDtoList);
		List<TCMmCostPlanDTO> dataList = costPlanDao.searchTCMmCostPlan(SearchParamUtil.getSearchParam(searchCostPlanDto));
		if (CollectionsUtils.isEmpty(dataList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		List<ResponseCreditRepaymentNoticeCreditDTO> creditList = new ArrayList<ResponseCreditRepaymentNoticeCreditDTO>();
		ResponseCreditRepaymentNoticeCreditDTO credit = null;
		Map<String, String> tempMap = new HashMap<String, String>();
		for (TCMmCostPlanDTO costPlanDTO : dataList) {
			if (SimpleStringUtils.hasText(tempMap.get(costPlanDTO.getTransferId()))) {
				continue;
			}
			credit = new ResponseCreditRepaymentNoticeCreditDTO();
			SimpleBeanUtils.copyBean2Bean(credit, costPlanDTO);
			credit.setCreditCode(costPlanDTO.getCreditorCode());
			credit.setLoanTerms(costPlanDTO.getCurrentTime().intValue());
			credit.setPaymentDate(DateTimeUtils.formatDate(costPlanDTO.getStartDate(), DateTimeUtils.DEFAULT_DATE_FORMAT_2));
			if (SimpleStringUtils.hasText(costPlanDTO.getCreditStatus())) {
				credit.setAdvanceSettleValue(costPlanDTO.getMatchAmount().add(costPlanDTO.getReturnAmount()));
				tempMap.put(costPlanDTO.getTransferId(), costPlanDTO.getTransferId());
			}

			creditList.add(credit);
		}

		if (CollectionsUtils.isEmpty(creditList)) {
			response.setResult(ResponseMessageEnum.FAILURE.getResult());
			response.setMessage("查询无记录");
			return response;
		}

		response.setId("CRN" + DateTimeUtils.formatDate(new Date(), DateTimeUtils.DEFAULT_DATE_TIME_FORMAT_2));
		response.setResult(ResponseMessageEnum.SUCCESS.getResult());
		response.setCreditList(creditList);

		return response;
	}
}
